/*
 * Copyright 2021 DataCanvas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.dingodb.proxy.handler;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import io.dingodb.sdk.service.entity.common.VectorIndexParameter;
import io.dingodb.sdk.service.entity.common.VectorIndexParameter.VectorIndexParameterNest.BruteforceParameter;
import io.dingodb.sdk.service.entity.common.VectorIndexParameter.VectorIndexParameterNest.DiskannParameter;
import io.dingodb.sdk.service.entity.common.VectorIndexParameter.VectorIndexParameterNest.FlatParameter;
import io.dingodb.sdk.service.entity.common.VectorIndexParameter.VectorIndexParameterNest.HnswParameter;
import io.dingodb.sdk.service.entity.common.VectorIndexParameter.VectorIndexParameterNest.IvfFlatParameter;
import io.dingodb.sdk.service.entity.common.VectorIndexParameter.VectorIndexParameterNest.IvfPqParameter;
import io.dingodb.sdk.service.entity.common.VectorIndexType;

import java.io.IOException;

public class VectorIndexParameterDeserializer extends JsonDeserializer<VectorIndexParameter> {

    @Override
    public VectorIndexParameter deserialize(
        JsonParser jsonParser, DeserializationContext deserializationContext
    ) throws IOException, JacksonException {
        ObjectCodec codec = jsonParser.getCodec();
        JsonNode node = codec.readTree(jsonParser);
        VectorIndexType vectorIndexType = VectorIndexType.valueOf(node.get("vectorIndexType").asText());
        VectorIndexParameter.VectorIndexParameterNest nest;
        switch (vectorIndexType) {
            case VECTOR_INDEX_TYPE_FLAT:
                nest = codec.treeToValue(node.get("flatParam"), FlatParameter.class);
                break;
            case VECTOR_INDEX_TYPE_IVF_FLAT:
                nest = codec.treeToValue(node.get("ivfFlatParam"), IvfFlatParameter.class);
                break;
            case VECTOR_INDEX_TYPE_IVF_PQ:
                nest = codec.treeToValue(node.get("ivfPqParam"), IvfPqParameter.class);
                break;
            case VECTOR_INDEX_TYPE_HNSW:
                nest = codec.treeToValue(node.get("hnswParam"), HnswParameter.class);
                break;
            case VECTOR_INDEX_TYPE_DISKANN:
                nest = codec.treeToValue(node.get("diskAnnParam"), DiskannParameter.class);
                break;
            case VECTOR_INDEX_TYPE_BRUTEFORCE:
                nest = codec.treeToValue(node.get("bruteForceParam"), BruteforceParameter.class);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + vectorIndexType);
        }
        return VectorIndexParameter.builder().vectorIndexType(vectorIndexType).vectorIndexParameter(nest).build();
    }
}
